package manager_web.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import manager_service.exception.ManagerNotExistException;
import manager_service.exception.UserNotExistException;
import manager_service.service.ManagerService;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.support.MultipartFilter;
import org.springframework.web.servlet.ModelAndView;
import test_common.constant.Constant;
import test_common.entity.Course;
import test_common.entity.Manager;
import test_common.entity.User;
import test_common.utils.ContentIsNull;
import test_common.utils.ResponseResult;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/manager")
public class ManagerController {
    @Reference
    private ManagerService managerService;


    @RequestMapping("/login")
    public ModelAndView login(Manager manager, HttpServletRequest request){

        ModelAndView mav=new ModelAndView();
        Manager manager1=null;
        String code=request.getParameter("code").trim();
        String code2=request.getSession().getAttribute("code").toString().trim();
        System.out.println(">>>>>>>" + code2);
            if (code.equals(code2)){
                try {
                    manager1=managerService.selectManagerByName(manager);
                    mav.addObject(manager1);
                    mav.setViewName("back_Home");
                    request.getSession().setAttribute("own",manager1);
                }
                catch (ManagerNotExistException e) {
                    e.printStackTrace();
                    mav.addObject("msg",e.getMessage());
                    mav.setViewName("back_login");
                }
            }
            else {
                mav.addObject("loginMsg","验证码错误");
                mav.setViewName("back_login");
            }
            return mav;
    }

    @RequestMapping("/showUsers")
    public ModelAndView showUsers(HttpServletRequest request){
            ModelAndView mav=new ModelAndView();
        String pageNo=request.getParameter("pageNo");
        String pageSize=request.getParameter("pageSize");
        //如果前端没有传递，使用默认值
        if (ContentIsNull.isNull(pageSize)) {
            pageSize= Constant.User_DEFAULT_PAGE_SIZE;
        }
        if (ContentIsNull.isNull(pageNo)){
            pageNo= Constant.User_DEFAULT_PAGE_NO;
        }

        int pageNoint=Integer.parseInt(pageNo);
        int pageSizeint=Integer.parseInt(pageSize);
        PageInfo<User> pageUsers = managerService.showUsers(pageNoint,pageSizeint);
        mav.setViewName("back_userSet");
        mav.addObject("pageUsers",pageUsers);
        return mav;
    }

    @ResponseBody
    @RequestMapping("/modifyUser")
    public String modifyUser(User user){
//        String id=user.getId().toString();
//        String nickname=user.getNickname();
//        String password=user.getPassword();
//        String role=user.getRole();
//        String email=user.getEmail();
        if (ContentIsNull.isNull(user.getId().toString())||ContentIsNull.isNull(user.getNickname())||ContentIsNull.isNull(user.getPassword())||
        ContentIsNull.isNull(user.getRole())||ContentIsNull.isNull(user.getEmail())){
            return "有内容为空！";
        }
        managerService.modifyUser(user);
             return "修改成功！";
    }

    @RequestMapping("/selectUser")
    @ResponseBody
    public User selectUser(HttpServletRequest request){
        String id=request.getParameter("id");
        try {
            User user= managerService.selectUser(id);
            return user;
        } catch (UserNotExistException e) {
            e.printStackTrace();
        }

        return null;
    }



    @RequestMapping("/searchUsers")
    public ModelAndView searchUsers( HttpServletRequest request){
        ModelAndView mav=new ModelAndView();
        String loginName=request.getParameter("loginName");
        String nickname=request.getParameter("nickname");
        String email=request.getParameter("email");
        String role=request.getParameter("role");
        String createDate=request.getParameter("createDate");
        String createDate2=request.getParameter("createDate2");
        String signDate=request.getParameter("signDate");
        String  signDate2=request.getParameter("signDate2");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String pageNo=request.getParameter("pageNo");
        String pageSize=request.getParameter("pageSize");
        //如果前端没有传递，使用默认值
        if (ContentIsNull.isNull(pageSize)) {
            pageSize= Constant.User_DEFAULT_PAGE_SIZE;
        }
        if (ContentIsNull.isNull(pageNo)){
            pageNo= Constant.User_DEFAULT_PAGE_NO;
        }

        if (ContentIsNull.isNull(createDate)){
            createDate="2020-05-01 00:00:00";
        }
        if (ContentIsNull.isNull(signDate)){
            signDate="2020-05-01 00:00:00";
        }
        if (ContentIsNull.isNull(createDate2)){
            createDate2= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        }
        if (ContentIsNull.isNull(signDate2)){
            signDate2= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        }
        try{
            List<User> searchUsers=managerService.searchUsers(loginName,nickname,email,role,
                     simpleDateFormat.parse(createDate),
                     simpleDateFormat.parse(createDate2),
                     simpleDateFormat.parse(signDate),
                     simpleDateFormat.parse(signDate2)
            );
//            request.setAttribute("searchUsers",searchUsers);
            mav.setViewName("user_search_result");
            mav.addObject("searchUsers",searchUsers);

        } catch (ParseException e) {
            e.printStackTrace();

        }
        return  mav;
    }

    @ResponseBody
    @RequestMapping("/modifyStatus")
    public ResponseResult modifyStatus(HttpServletRequest request){
        ResponseResult result=new ResponseResult();
        String userId=request.getParameter("userId");
        System.out.println(">>>>>>>>>>"+userId);
        try {
            managerService.modifyUserStatus(Integer.parseInt(userId));
            result.setStatus(1);
            result.setMessage("状态修改成功");
        } catch (NumberFormatException e) {
            e.printStackTrace();
            result.setStatus(-1);
            result.setMessage("状态修改失败");

        }
        return result;
    }

    @RequestMapping("/back_userSet")
    public String showBackUserSet(){
        return "back_userSet";
    }


}
